package user;

import java.awt.*;
import java.awt.geom.AffineTransform;
import loading.ImageLoader;

import math.Vector2D;

/**
 * 
 * @author Ryan Hill
 */
public class Button 
{
        /**
         * Position of button
         */
        private Vector2D pos;
        /**
         * Image to be drawn
         */
        private ImageLoader image;
        
        private String desc;
        private boolean enabled;
        
        private boolean visible;
        
        /**
         * Constructor
         * 
         * @param p location on screen
         * @param img image to be seen
         */
        public Button( Vector2D p, ImageLoader img )
        {
            pos = p;
            image = img;
            desc = "";
            visible = true;
            enabled = true;
        }
        
        public void setEnabled( boolean tf )
        {
        	enabled = tf;
        }
        
        public boolean isEnabled()
        {
        	return enabled;
        }
        
        public String getDescription()
        {
        	return desc;
        }
        
        public void setDescription( String s )
        {
        	desc = s;
        }
        
        public void setVisible( boolean v )
        {
        	visible = v;
        }
        
        public boolean getVisible()
        {
        	return visible;
        }
        
        public ImageLoader getImage()
        {
        	return image;
        }
        
        public Vector2D getLocation()
        {
        	return pos;
        }
        /**
         * Draw the button
         * 
         * @param g2d graphics to draw to
         */
        public void render( Graphics2D g2d )
        {
                //draw only if visible
                if( visible && enabled )
                	image.draw( g2d, AffineTransform.getTranslateInstance( pos.getX(), pos.getY() ) );
                else if( visible && !enabled )
                {
            		g2d.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.2f ) );

                	image.draw( g2d, AffineTransform.getTranslateInstance( pos.getX(), pos.getY() ) );
                    
            		g2d.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 1.0f ) );
                }
        }
        
        /**
         * @param v mouse location
         * @return true if the mouse moves within the boundaries of the image and image is visible, false otherwise
         */
        public boolean mouseOver( Vector2D v )
        {
        	return click( v );
        }
        
        /**
         * @param v mouse location
         * @return true if the mouse clicks within the boundaries of the image and image is visible, false otherwise
         */
        public boolean click( Vector2D v )
        { 
        	return ( new Rectangle( (int)pos.getX(), (int)pos.getY(), image.width(), image.height() ) ).contains( v.getX(), v.getY() ) && visible && enabled;
        }
}
